<?php
namespace app\seller\controller;
use think\Input;
class Order extends Common{
    public function initialize(){
        parent::initialize();   
        $this->assign('shoplist',cache('shoplist'));   
        $this->assign('cionlist',cache('cionlist')); 
    }
    public function index(){
    	if(request()->isPost()){ 
            if(session('seller.id') == 23 || session('seller.id') == 25 || session('seller.id') == 29 || session('seller.id') == 30){
                    $where=[];
                    $map='';
                    $shopid = session('seller.id');
                    if($shopid) $where['shopid']=$shopid; 
                    $timess = input('post.timess');            
                    if($timess){ 
                        $timess=explode(" - ",$timess);
                        $starttime=strtotime($timess[0]);
                        $endtime=strtotime($timess[1]);    
                        $map= [$starttime,$endtime];  
                    }
                    $timequan = input('post.timequan');
                    if($timequan) $map= ''.$timequan.''; 
                    $orderid = input('post.orderid');
                    if($orderid) $where['orderid']=$orderid;  
                    $orderid_out = input('post.orderid_out');
                    if($orderid_out) $where['orderid_out']=$orderid_out;   
                    $state = input('post.state');
                    if($state === "-1"){
                        if($state)$where['state']="0";
                    }else{
                       if($state)$where['state']=$state; 
                    }            
                    $paytype = input('post.paytype');
                    if($paytype)$where['paytype']=$paytype;  
                       
                    $page =input('page')?input('page'):1;
                    $pageSize =input('limit')?input('limit'):config('pageSize');
                    $list=db('apporder')
                        ->where($where)->whereTime('addtime',$map)->order('id desc')
                        ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                        ->toArray();   
                    foreach ($list['data'] as $k=>$v){
                        $list['data'][$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                        if($v['paytime']) $list['data'][$k]['paytime'] = date('Y-m-d H:i',$v['paytime']);
                        if($v['state']===0){ 
                            $list['data'][$k]['state_name'] ='<span class="red">待支付</span>';
                        }else if($v['state']===2){
                            $list['data'][$k]['state_name'] ='<span class="gre">已确认</span>';
                        }
                        if($v['paytype']===1){ 
                            $list['data'][$k]['paytype'] ='<span class="blue">支付宝</span>';
                            $list['data'][$k]['payname'] ='<span class="blue">支付宝</span>';
                            $list['data'][$k]['cionname'] ='<span class="blue">比特币</span>';
                        }else if($v['paytype']===2){
                            $list['data'][$k]['paytype'] ='<span class="gre">微信</span>';
                            $list['data'][$k]['payname'] ='<span class="gre">微信</span>';
                            $list['data'][$k]['cionname'] ='<span class="blue">比特币</span>';
                        }
                        if($v['points']===0){
                            $list['data'][$k]['points_name'] ='<span class="orange">待上分</span>';
                        }else if($v['points']===1){
                            $list['data'][$k]['points_name'] ='<span class="green">自动上分</span>';
                        }else if($v['points']===2){
                            $list['data'][$k]['points_name'] ='<span class="green">手动上分</span>';
                        }
                        if($v['fei_user_jie']===0) $list['data'][$k]['fei_user']='<span class="orange" title="待结算">'.$v['fei_user'].'</span>';
                        if($v['fei_shop_jie']===0) $list['data'][$k]['fei_shop']='<span class="orange" title="待结算">'.$v['fei_shop'].'</span>';
                        if($v['fei_agent_jie']===0) $list['data'][$k]['fei_agent']='<span class="orange" title="待结算">'.$v['fei_agent'].'</span>';
                        if($v['fei_shop_jie']===0){
                            $list['data'][$k]['fei_jie'] ='<span class="red">待结算</span>';
                        }else if($v['fei_shop_jie']===1){
                            $list['data'][$k]['fei_jie'] ='<span class="gre">已结算</span>';
                        } 

                    }
                    return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
                 
            }  
            $where['o.shopid']=session('seller.id');
            $map='';
            // $userid = input('post.userid');
            // if($userid) $where['userid']=$userid;
            $cionid = input('post.cionid');
            if($cionid) $where['cionid']=$cionid;
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1])+86399;    
                $map= [$starttime,$endtime];  
            }
            $timequan = input('post.timequan');
            if($timequan) $map= ''.$timequan.''; 
            $orderid = input('post.orderid');
            if($orderid) $where['orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['orderid_out']=$orderid_out;
            $channelid = input('post.channelid');
            if($channelid) $where['channelid']=$channelid;   

            $state = input('post.state');
            if($state) $where['state']=$state;
            $points = input('post.points');
            if($points)$where['points']=$points;   
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('channel_order')->alias('o')
                ->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')
                ->field('o.*,ub.name as payname,ub.bank_no,ub.bank_name')->where('o.state','neq','-1')
                ->where($where)->whereTime('o.addtime',$map)->order('o.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();   
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);
                if($v['paytime']) $list['data'][$k]['paytime'] = date('Y-m-d H:i',$v['paytime']);
                if($v['state']===0){ 
                    $payurl=url('shop/api/pay',['orderid'=>$v['orderid'],'appid'=>$v['shopappid']]);
                	$list['data'][$k]['state_name'] ='<span class="red">待支付</span>';
                }else if($v['state']===1){
                    $list['data'][$k]['state_name'] ='<span class="blue">待确认</span>';
                }else if($v['state']===2){
                    $list['data'][$k]['state_name'] ='<span class="gre">已确认</span>';
                }else if($v['state']===3){
                    $list['data'][$k]['state_name'] ='<span class="hui">已取消</span>';
                } 
                if($v['points']===0){ 
                    $list['data'][$k]['points_name'] ='<span class="red">待上分</span>';
                }else if($v['points']===1){
                    $list['data'][$k]['points_name'] ='<span class="gre">自动上分</span>';
                }else if($v['points']===2){
                    $list['data'][$k]['points_name'] ='<span class="gre">手动上分</span>';
                }
                if($v['fei_shop_jie']===0){
                    $list['data'][$k]['fei_jie'] ='<span class="red">待结算</span>';
                }else if($v['fei_shop_jie']===1){
                    $list['data'][$k]['fei_jie'] ='<span class="gre">已结算</span>';
                } 
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        // 订单汇总
        // $payorder=[];
        // $payorder['no']=db('channel_order')->where('shopid',session('seller.id'))->where('state',0)->sum('money');
        // $payorder['que']=db('channel_order')->where('shopid',session('seller.id'))->where('state',1)->sum('money');
        // $payorder['yes']=db('channel_order')->where('shopid',session('seller.id'))->where('state',2)->sum('money'); 
        // $this->assign('payorder',$payorder);
        $this->assign('title','订单管理');
        return $this->fetch();
    }  
    public function ordermin(){   
        $this->assign('userid',input('userid')); 
        $this->assign('channelid',input('channelid')); 
        return $this->fetch();
    } 

     public function total(){
        if(request()->isPost()){ 
            $map='';
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]);
                if($starttime >= $endtime){
                    return $result = ['code'=>-1,'msg'=>'开始日期晚于结束日期!','rel'=>1];
                }    
                $map= [$starttime,$endtime];  
            }
            // $timequan = input('post.timequan');
            // if($timequan) $map= ''.$timequan.''; 
            $orderid = input('post.orderid');
            if($orderid) $where['orderid']=$orderid; 
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['orderid_out']=$orderid_out;  
            $channelid = input('post.channelid');
            if($channelid) $where['channelid']=$channelid;   

            $state = input('post.state');
            if($state === '1'){
                if($state) $where['state']=0;
            }else{
                if($state) $where['state']=$state;
            }
            
            $shopid=session('seller.id');
            $s_today = strtotime(date('Y-m-d',time()));
            $e_today = strtotime(date('Y-m-d',time()))+86399;
            if($shopid){
                $payorder['no']=db('channel_order')->where('shopid',$shopid)->where('state',0)->where($where)->whereTime('addtime',$map)->sum('money');
                $payorder['yes']=db('channel_order')->where('shopid',$shopid)->where('state',2)->where($where)->whereTime('addtime',$map)->sum('money');
                $payorder['s_yjie']=db('channel_order')->where('shopid',$shopid)->where('state',2)->where($where)->whereTime('addtime',$map)->sum('fei_shop');
                // $payorder['s_yjie']=db('channel_order')->where('shopid',$shopid)->where('state',2)->where('fei_shop_jie',1)->where($where)->whereTime('addtime',$map)->sum('fei_shop');
                $payorder['income']=$payorder['yes']-$payorder['s_yjie'];

                //当日收入
                // $payorder['no']=db('channel_order')->where('shopid',$shopid)->where('state',0)->whereTime('addtime',[$s_today,$e_today])->sum('money');
                $payorder['today_yes']=db('channel_order')->where('shopid',$shopid)->where('state',2)->whereTime('addtime',[$s_today,$e_today])->sum('money');
                 $payorder['today_s_yjie']=db('channel_order')->where('shopid',$shopid)->where('state',2)->whereTime('addtime',[$s_today,$e_today])->sum('fei_shop');
                // $payorder['today_s_yjie']=db('channel_order')->where('shopid',$shopid)->where('state',2)->whereTime('addtime',[$s_today,$e_today])->where('fei_shop_jie',1)->sum('fei_shop');
                $payorder['today_income']=$payorder['today_yes']-$payorder['today_s_yjie'];
                // var_dump($payorder['today_yes']);die;
                $payorders[0]=$payorder;
                return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$payorders,'rel'=>1];
            }else{
                return $result = ['code'=>1,'msg'=>'获取失败，缓存失败!','data'=>$payorders,'rel'=>1];
            }
       
        } 
    }
}